package cn.nawang.ebeim.ui.util;

import com.google.gson.Gson;

import cn.nawang.ebeim.ui.bean.BackupLogWrapper;
import cn.nawang.ebeim.ui.bean.DataSourceWrapper;
import cn.nawang.ebeim.ui.bean.UpdatePassword;
import cn.nawang.ebeim.ui.bean.User;

public class ApiUtil {

  public static String apiServer = PropertiesUtil.read("apiServer");
  public static String host = PropertiesUtil.read("host");
  public static int port = Integer.parseInt(PropertiesUtil.read("port"));
  public static final String TOKEN = "&token=tokennawang";

  public static RestUtil createRestUtil() {
    return new RestUtil(host, port);
  }

  public static String getIsOnline(String userId) {
    String uri = ApiConstants.URI_ISONLINE + "?userId=" + userId;
    uri += TOKEN;
    RestUtil rest = createRestUtil();
    return rest.doGet(uri);
  }

  public static String getDataSourcesByCreateUserIdByPage(String createUserId, String offset,
      String pagesize, String dsType, String status, String name, String terminalId) {
    String uri = ApiConstants.URI_DS_BY_CREATEUSERID_PAGES + "?createUserId=" + createUserId
        + "&startPos=" + offset + "&pageSize=" + pagesize + "&dsType=" + dsType + "&name=" + name
        + "&terminalId=" + terminalId;
    uri += TOKEN;
    RestUtil rest = createRestUtil();
    return rest.doGet(uri);
  }

  public static String getDataSourcesByCreateUserIdAndDsType(String createUserId, String dsType) {
    String uri = ApiConstants.URI_DATASOURCE_BY_CREATE_USERID_AND_DS_TYPE + "?createUserId=" + createUserId
       + "&dsType=" + dsType ;
    uri += TOKEN;
    RestUtil rest = createRestUtil();
    return rest.doGet(uri);
  }

  public static String getDataSources(String terminalId) {
    String uri = ApiConstants.URI_DS_BY_TERMINAL + "?terminalId=" + terminalId;
    uri += TOKEN;
    RestUtil rest = createRestUtil();
    return rest.doGet(uri);
  }

  public static String getBackupLog(BackupLogWrapper backupLogWrapper) {
    String data = new Gson().toJson(backupLogWrapper);
    String uri = ApiConstants.URI_BACKUP_LOG_LIST;
    uri = uri + "?" + TOKEN;
    RestUtil rest = createRestUtil();
    return rest.doPost(uri, data);
  }

  public static String getFileChangeLogs(String fid) {
    String uri = ApiConstants.URI_FILE_CHANGE_LOG_LIST + "?fileId=" + fid;
    uri += TOKEN;
    RestUtil rest = createRestUtil();
    return rest.doGet(uri);
  }

  public static String getPushMessage(String id) {
    String uri = ApiConstants.URI_PUSH_MESSAGE_LIST + "?id=" + id;
    uri += TOKEN;
    RestUtil rest = createRestUtil();
    return rest.doGet(uri);
  }

  public static String getDataSourceByDsId(String dsId) {
    String uri = ApiConstants.URI_DS_BY_ID + "?id=" + dsId;
    uri += TOKEN;
    RestUtil rest = createRestUtil();
    return rest.doGet(uri);
  }

  public static String addDataSource(DataSourceWrapper ds) {
    String uri = ApiConstants.URI_ADD_DATASOURCE;
    uri += "?" + TOKEN;
    RestUtil rest = createRestUtil();
    String data = new Gson().toJson(ds);
    System.out.println("============================+++++++++++++++++++++++++");
    System.out.println(data);
    return rest.doPost(uri, data);
  }

  public static String getShortMessage(String username) {
    String uri = ApiConstants.URI_SMS_SEND + "?username=" + username;
    uri += TOKEN;
    RestUtil rest = createRestUtil();
    return rest.doGet(uri);
  }

  public static String checkShortMessage(String username, String code) {
    String uri = ApiConstants.URI_SMS_CHECK + "?username=" + username + "&code=" + code;
    uri += TOKEN;
    RestUtil rest = createRestUtil();
    return rest.doGet(uri);
  }

  /**
   * 
   * @author fanyb
   * @Description: 修改密码（需要短信验证码的接口）
   * @param userId
   * @param oldPassword
   * @param newPassword
   * @param code
   * @return String
   * @date Oct 28, 201512:05:01 PM
   *
   * @修改人
   * @修改日期
   */
  public static String updatePasswordShortMessageCheck(UpdatePassword updatePassword) {
    String uri = ApiConstants.URI_UPDATE_PASSWORD;
    uri += "?" + TOKEN;
    RestUtil rest = createRestUtil();
    String data = new Gson().toJson(updatePassword);
    return rest.doPost(uri, data);
  }

  /**
   * 
   * @author fanyb
   * @Description: 无授权验证
   * @return String
   * @date Oct 28, 20151:12:02 PM
   *
   * @修改人
   * @修改日期
   */
  public static String noAuthorizetionCheck(String username) {
    String uri = ApiConstants.URI_NO_AUTH + "?username=" + username;
    uri += TOKEN;
    RestUtil rest = createRestUtil();
    return rest.doGet(uri);
  }

  /**
   * 
   * @author fanyb
   * @Description: key无关联验证
   * @param username
   * @param code
   * @return String
   * @date Oct 28, 20151:40:50 PM
   *
   * @修改人
   * @修改日期
   */
  public static String keyNoBindCheck(String username, String code, String key) {
    String uri =
        ApiConstants.URI_KEY_NO_BIND + "?username=" + username + "&code=" + code + "&key=" + key;
    uri += TOKEN;
    RestUtil rest = createRestUtil();
    return rest.doGet(uri);
  }

  /**
   * 
   * @author fanyb
   * @Description: key关联验证
   * @param username
   * @param code
   * @param bindDevice
   * @param bindDeviceName
   * @return String
   * @date Oct 28, 20151:49:25 PM
   *
   * @修改人
   * @修改日期
   */
  public static String keyBindCheck(String username, String code, String bindDevice,
      String bindDeviceName, String key) {
    String uri = ApiConstants.URI_KEY_BIND + "?username=" + username + "&code=" + code
        + "&bindDevice=" + bindDevice + "&bindDeviceName=" + bindDeviceName + "&key=" + key;
    uri += TOKEN;
    RestUtil rest = createRestUtil();
    return rest.doGet(uri);
  }

  /**
   * 
   * @author fanyb
   * @Description: 授权码不关联
   * @param username
   * @param code
   * @return String
   * @date Oct 28, 20152:15:15 PM
   *
   * @修改人
   * @修改日期
   */
  public static String authorizationCodeNoBindCheck(String username, String code) {
    String uri = ApiConstants.URI_AUTH_CODE_NO_BIND + "?username=" + username + "&code=" + code;
    uri += TOKEN;
    RestUtil rest = createRestUtil();
    return rest.doGet(uri);
  }

  /**
   * 
   * @author fanyb
   * @Description: 授权码绑定验证
   * @param username
   * @param code
   * @param bindDevice
   * @param bindDeviceName
   * @return String
   * @date Oct 28, 20152:16:04 PM
   *
   * @修改人
   * @修改日期
   */
  public static String authorizationCodeBindCheck(String username, String code, String bindDevice,
      String bindDeviceName) {
    String uri = ApiConstants.URI_AUTH_CODE_BIND + "?username=" + username + "&code=" + code
        + "&bindDevice=" + bindDevice + "&bindDeviceName=" + bindDeviceName;
    uri += TOKEN;
    RestUtil rest = createRestUtil();
    return rest.doGet(uri);
  }

  public static String updateDataSource(DataSourceWrapper ds) {
    String uri = ApiConstants.URI_UPDATE_DATASOURCE;
    uri += "?" + TOKEN;
    RestUtil rest = createRestUtil();
    String data = new Gson().toJson(ds);
    System.out.println("============================+++++++++++++++++++++++++");
    System.out.println(data);
    return rest.doPost(uri, data);
  }

  public static String login(User user) {
    RestUtil rest = createRestUtil();
    String rs = rest.doGet(ApiConstants.URI_LOGIN + "?username=" + user.getUsername() + "&password="
        + user.getPassword() + "&code=" + user.getCode() + TOKEN);
    return rs;
  }

  public static String findByUsername(String username) {
    String uri = ApiConstants.URI_USER_BY_USERNAME + "?username=" + username;
    uri += TOKEN;
    RestUtil rest = createRestUtil();
    return rest.doGet(uri);
  }

  public static String findFirstPageTotalByUserId(String userId) {
    String uri = ApiConstants.URI_USER_BY_USER_ID + "?userId=" + userId;
    uri += TOKEN;
    RestUtil rest = createRestUtil();
    return rest.doGet(uri);
  }

  public static String findBindDeviceNameByTerminalId(String createUserId) {
    String uri = ApiConstants.URI_DATASOURCE_DEVICE_NAME + "?createUserId=" + createUserId;
    uri += TOKEN;
    RestUtil rest = createRestUtil();
    return rest.doGet(uri);
  }

  public static String getSafeBoxInfo(String createUserId, String terminalId, String dsType) {
    String uri = ApiConstants.URI_SAVE_BOX_INFO + "?createUserId=" + createUserId + "&terminalId="
        + terminalId + "&dsType=" + dsType;
    uri += TOKEN;
    RestUtil rest = createRestUtil();
    return rest.doGet(uri);
  }

  public static String updateDatasourcesStatus(String ids, int dsStatus) {
    String uri =
        ApiConstants.URI_UPDATE_DATASOURCES_STATUS + "?ids=" + ids + "&dsStatus=" + dsStatus;
    uri += TOKEN;
    RestUtil rest = createRestUtil();
    return rest.doGet(uri);
  }

  public static String findDatasourcesStatusResult(String ids) {
    String uri = ApiConstants.URI_FIND_DATASOURCES_STATUS_RESULT + "?ids=" + ids;
    uri += TOKEN;
    RestUtil rest = createRestUtil();
    return rest.doGet(uri);
  }

  public static void main(String[] args) throws Exception {

    // String rs = ApiUtil.getShortMessage("15759769179");
    // System.out.println(rs);
    // String rs2 = ApiUtil.checkShortMessage("15759769179", "778258");
    // System.out.println(rs2);
    System.out.println(ApiUtil.getIsOnline("2"));

  }

}
